<template>
  <div class="employees" v-loading="loading">
    <el-card class="btn">
      <el-button
        type="primary"
        @click="goImport"
        v-control="'employees_import_buttton'"
        >导入</el-button
      >
      <el-button type="primary" @click="add" v-control="'employees_add_buttton'"
        >+新增员工</el-button
      >
    </el-card>
    <el-card>
      <el-table :data="list">
        <el-table-column label="序号">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="头像" prop="staffPhoto">
          <template v-slot="{ row }">
            <img
              :src="row.staffPhoto"
              class="avatar"
              @click="imgClick(row.staffPhoto)"
              alt=""
              v-imgerror="errorUrl"
            />
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile"></el-table-column>
        <el-table-column label="工号" prop="workNumber"></el-table-column>
        <!-- :formatter='formatter' -->
        <el-table-column label="聘用形式" prop="formOfEmployment">
          <!-- <template v-slot="{ row }">
            <div v-hireType="row.formOfEmployment">
              {{ row.formOfEmployment | formatType }}
            </div>
          </template> -->
        </el-table-column>
        <el-table-column label="部门" prop="departmentName"></el-table-column>
        <el-table-column label="入职时间" prop="timeOfEntry">
          <!-- <template v-slot="{ row }">
            <div>
              {{ row.timeOfEntry | formatTime }}
            </div>
          </template> -->
        </el-table-column>
        <!-- <el-table-column label="是否在职" prop=""></el-table-column> -->
        <el-table-column label="状态" prop="enableState"></el-table-column>
        <el-table-column label="操作" width="3.75rem" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="goDetail(row.id)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="roleClick(row.id)">角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.page"
          :page-sizes="[1, 10, 20, 100]"
          :page-size="page.size"
          layout="total, sizes,prev,pager,next,jumper"
          :total="page.total"
        >
        </el-pagination>
      </div>
    </el-card>
    <Dialog :isShow.sync="show" @getData="getData"></Dialog>
    <!-- 头像二维码 -->
    <el-dialog title="二维码" :visible.sync="showQrcode">
      <div style="text-align: center">
        <canvas ref="canvas"></canvas>
      </div>
    </el-dialog>
    <!-- 分配角色组件 -->
    <DialogRole ref="dialogRole"></DialogRole>
  </div>
</template>

<script>
import { sysUser, sysUserDel } from "@/api/employees.js";
import employees from "@/assets/constant/employees";
import Dialog from "./components/dialog.vue";
import DialogRole from "./components/dialogRole.vue";

import QrCode from "qrcode";
export default {
  components: {
    Dialog,
    DialogRole,
  },
  data() {
    return {
      showQrcode: false,
      errorUrl: require("@/assets/common/head.jpg"),
      loading: false,
      show: false,
      list: [],
      page: {
        total: 100,
        page: 1,
        size: 1,
      },
      form: {
        username: "",
        workNumber: "",
        mobile: "",
        formOfEmployment: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      },
    };
  },
  created() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await sysUser(this.page);
      console.log("res:", res);

      res.data.rows.forEach((item) => {
        item.formOfEmployment = this.formatter(item.formOfEmployment);
      });

      this.list = res.data.rows;
      this.page.total = res.data.total;
    },
    handleSizeChange(size) {
      this.page.size = size;
      this.page.page = 1;
      this.getData();
    },
    handleCurrentChange(page) {
      this.page.page = page;
      this.getData();
    },
    formatter(id) {
      const result = employees.hireType.find((item) => {
        return item.id === id;
      });
      return result ? result.value : "-";
    },
    // 新增点击事件
    add() {
      this.show = true;
    },
    del(id) {
      this.$confirm("您确实删除该员工吗？", "提示").then(async () => {
        await sysUserDel(id);
        this.$message.success("删除成功");
        this.handleCurrentChange(1);
      });
    },
    goImport() {
      this.$router.push(
        "/employees/import?name=" +
          encodeURIComponent("员工") +
          `&redirect=${encodeURIComponent("/employees")}`
      );
    },
    goDetail(id) {
      this.$router.push("/employees/details/" + id);
    },
    imgClick(imgurl) {
      if (!imgurl) {
        this.$message.error("暂无照片");
        return;
      }
      this.showQrcode = true;
      this.$nextTick(() => {
        QrCode.toCanvas(this.$refs.canvas, imgurl, {
          width: 300,
          height: 300,
        });
      });
    },
    // 角色点击
    roleClick(id) {
      this.$refs.dialogRole.isShow = true;
      this.$refs.dialogRole.getUserInfo(id);
    },
  },
};
</script>

<style lang="scss" scoped>
.ememployees {
  padding: 0.1875rem;
  .ben {
    text-align: right;
    margin-bottom: 0.1875rem;
  }
  .page {
    padding-top: 0.375rem;
    text-align: right;
  }
}
</style>
